3 * Classes used to send headers and cookies back to the user
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
26 class FauxResponse
extends WebResponse
{
28 private $cookies = [];
32 * Stores a HTTP header
33 * @param string $string Header to output
34 * @param bool $replace Replace current similar header
35 * @param null|int $http_response_code Forces the HTTP response code to the specified value.
37 public function header( $string, $replace = true, $http_response_code = null ) {
38 if ( substr( $string, 0, 5 ) == 'HTTP/' ) {
39 $parts = explode( ' ', $string, 3 );
40 $this->code
= intval( $parts[1] );
42 list( $key, $val ) = array_map( 'trim', explode( ":", $string, 2 ) );
44 $key = strtoupper( $key );
46 if ( $replace ||
!isset( $this->headers
[$key] ) ) {
47 $this->headers
[$key] = $val;
51 if ( $http_response_code !== null ) {
52 $this->code
= intval( $http_response_code );
58 * @param int $code Status code
60 public function statusHeader( $code ) {
61 $this->code
= intval( $code );
64 public function headersSent() {
69 * @param string $key The name of the header to get (case insensitive).
70 * @return string|null The header value (if set); null otherwise.
72 public function getHeader( $key ) {
73 $key = strtoupper( $key );
75 return $this->headers
[$key] ??
null;
79 * Get the HTTP response code, null if not set
83 public function getStatusCode() {
88 * @param string $name The name of the cookie.
89 * @param string $value The value to be stored in the cookie.
90 * @param int|null $expire Ignored in this faux subclass.
91 * @param array $options Ignored in this faux subclass.
93 public function setCookie( $name, $value, $expire = 0, $options = [] ) {
94 global $wgCookiePath, $wgCookiePrefix, $wgCookieDomain;
95 global $wgCookieSecure, $wgCookieExpiration, $wgCookieHttpOnly;
97 $options = array_filter( $options, function ( $a ) {
100 'prefix' => $wgCookiePrefix,
101 'domain' => $wgCookieDomain,
102 'path' => $wgCookiePath,
103 'secure' => $wgCookieSecure,
104 'httpOnly' => $wgCookieHttpOnly,
108 if ( $expire === null ) {
109 $expire = 0; // Session cookie
110 } elseif ( $expire == 0 && $wgCookieExpiration != 0 ) {
111 $expire = time() +
$wgCookieExpiration;
114 $this->cookies
[$options['prefix'] . $name] = [
115 'value' => (string)$value,
116 'expire' => (int)$expire,
117 'path' => (string)$options['path'],
118 'domain' => (string)$options['domain'],
119 'secure' => (bool)$options['secure'],
120 'httpOnly' => (bool)$options['httpOnly'],
121 'raw' => (bool)$options['raw'],
126 * @param string $name
127 * @return string|null
129 public function getCookie( $name ) {
130 if ( isset( $this->cookies
[$name] ) ) {
131 return $this->cookies
[$name]['value'];
137 * @param string $name
140 public function getCookieData( $name ) {
141 return $this->cookies
[$name] ??
null;
147 public function getCookies() {
148 return $this->cookies
;